package com.pig.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pig.pojo.goods.Goods;
import com.pig.service.goods.CategoryService;
import com.pig.service.goods.GoodsService;
import com.pig.util.PageObject;
import com.pig.util.SysResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/goods/")
public class GoodsControllerInManage {
    @Reference
    private GoodsService goodsService;
    @Reference
    private CategoryService categoryService;

    @GetMapping("/doFindAllGoods")
    public SysResult doFindAllGoods(String title, Integer pageCurrent, Integer categoryId, Integer status, Integer pageSize) {
        PageObject<Goods> list = goodsService.findGoodsByTitle(title, status, categoryId, pageCurrent, pageSize);
        return SysResult.success(list);
    }

    @GetMapping("/doFindGoodsPage")
    public SysResult doFindGoodsPage(String title, Integer pageCurrent, Integer pageSize) {
        PageObject goodsPages = goodsService.findGoodsPages(title, pageCurrent, pageSize);
        return SysResult.success(goodsPages);
    }

    @GetMapping("/doFindGoodById")
    public SysResult doFindGoodsById(Integer goodsId) {
        Map<String, Object> goodsById = goodsService.findGoodsById(goodsId);
        return SysResult.success("调用成功！", goodsById);
    }

    @GetMapping("/doDelGoods")
    public SysResult doDelGoods(Integer goodsId) {
        goodsService.delGoods(goodsId);
        return SysResult.success("删除成功！");
    }

    @GetMapping("/doSetGoodsStatus")
    public SysResult doSetGoodsStatus(Integer goodsId, Integer status) {
        goodsService.setGoodsStatus(goodsId, status);
        return SysResult.success("设定成功！");
    }

    @PostMapping("/doSaveGoods")
    public SysResult doSaveGoods(Goods goods) {
        goodsService.insertGoods(goods);
        return SysResult.success("添加成功！");
    }

    @RequestMapping("/doFindAllCategory")
    public SysResult doFindAllCategory() {
        return SysResult.success(categoryService.findAllCategory());
    }

    @RequestMapping("/doUpdateGoods")
    public SysResult doUpdateGoods(Goods goods) {
        goodsService.UpdateGoods(goods);
        return SysResult.success();
    }
}
